package com.apps.sdses.flink141.test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jiwei
 * @description
 * @date 2024/1/25 9:41
 */
public class FieldValuesExtractor {

    public static Map<String, List<Double>> extractFieldValues(List<JsonNode> elements, List<String> fieldList) {
        ObjectMapper objectMapper = new ObjectMapper();

        return fieldList.stream()
                .collect(Collectors.toMap(
                        fieldName -> fieldName,
                        fieldName -> elements.stream()
                                .map(element -> element.get(fieldName).asDouble())
                                .collect(Collectors.toList())
                ));
    }

    public static void main(String[] args) {
        List<String> jsonData = Arrays.asList(
                "{\"sid\": \"1001\", \"ygwd\": 34.2, \"yhwd\": 22.2}",
                "{\"sid\":\"1002\", \"ygwd\":33.2, \"yhwd\": 44.4}"
        );

        List<JsonNode> elements = jsonData.stream()
                .map(jsonString -> {
                    try {
                        return new ObjectMapper().readTree(jsonString);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                })
                .collect(Collectors.toList());

        List<String> fieldList = Arrays.asList("ygwd", "yhwd");

        Map<String, List<Double>> result = extractFieldValues(elements, fieldList);

        // 打印结果
        result.forEach((fieldName, values) -> System.out.println(fieldName + ": " + values));
    }
}
